package com.vwa.DeepSeekR1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

@SpringBootApplication
public class DeepSeekR1Application {

	@Value("${myapp.home.url}")
	private String homeAddressUrl;

	public static void main(String[] args) {
		SpringApplication.run(DeepSeekR1Application.class, args);
	}

	@EventListener(ApplicationReadyEvent.class)
	public void openBrowserAfterStartup() {
		System.out.println("Spring Boot 应用已启动，正在打开浏览器...");
		System.out.println("Home URL: " + homeAddressUrl);
		// 设置要打开的 URL
		//String url = "http://localhost:8082/index.html";

		// 判断当前系统是否支持 Desktop
		if (Desktop.isDesktopSupported()) {
			Desktop desktop = Desktop.getDesktop();
			if (desktop.isSupported(Desktop.Action.BROWSE)) {
				try {
					// 打开浏览器
					desktop.browse(new URI(homeAddressUrl));
				} catch (IOException | URISyntaxException e) {
					e.printStackTrace();
				}
			}
		} else {
			// 如果不支持 Desktop，尝试使用命令行打开浏览器
			Runtime runtime = Runtime.getRuntime();
			try {
				if (System.getProperty("os.name").toLowerCase().contains("win")) {
					// Windows 系统
					runtime.exec("rundll32 url.dll,FileProtocolHandler " + homeAddressUrl);
				} else if (System.getProperty("os.name").toLowerCase().contains("mac")) {
					// macOS 系统
					runtime.exec("open " + homeAddressUrl);
				} else {
					// Linux 或其他系统
					runtime.exec("xdg-open " + homeAddressUrl);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
